package com.bisp.bgame.plugin;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.bisp.bgame.AddActivity;
import com.bisp.bgame.GameActivity;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class ChangeActivityPlugin extends Plugin {

	public static final String ACTION = "ChangeActivity";
	//private static String MY_ACTION = "GameActivity";
	public String callbackId;
	@Override
	public PluginResult execute(String action, JSONArray data, String callbackId) {
		// TODO Auto-generated method stub
		this.callbackId = callbackId;
		PluginResult.Status status = PluginResult.Status.OK;
		
		String result = "";
		if (ACTION.equals(action)) {
			try {
				String gameID = data.getString(0);
				ChangeActivity(gameID);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			return new PluginResult(status, result);
		}
		return new PluginResult(status, result);
	}
	
	public void ChangeActivity(String gameID)throws Exception {
		Bundle data = new Bundle();
		data.putString("gameID", gameID);
		
		//Intent newIntent = new Intent(this.ctx,GameActivity.class);
		Intent newIntent = new Intent(this.ctx,GameActivity.class);
		Log.v("Allven",gameID);
		newIntent.putExtras(data);
		
		try{
			this.ctx.startActivityForResult((Plugin)this, newIntent, 0);
		}catch (Exception e) {
			Log.v("Allven", "跳转失败："+e.toString());// TODO: handle exception
			
		}
		//startActivity(newIntent);
		
	}

}

